package com.shop.service;

import com.shop.entity.MerchantBasket;
import com.shop.entity.MerchantGoods;
import com.shop.entity.Message;
import com.shop.feign.MessageFeign;
import com.shop.mapper.MerchantBasketMapper;
import com.shop.model.vo.Rsp;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (MerchantBasket)表服务接口
 *
 * @author makejava
 * @since 2020-11-24 10:20:18
 */
@Service
@AllArgsConstructor
public class MerchantBasketService {

    private final MerchantBasketMapper merchantBasketMapper;
    private final MessageFeign messageFeign;

    /**
     * 获取对应商户的购物车
     *
     * @return
     */
    public Rsp getBasketList(Integer userId) {
        List<MerchantBasket> basketList = merchantBasketMapper.getBasketList(userId);
        List<Integer> basketMerchantList = new ArrayList<>();
        for (MerchantBasket merchantBasket : basketList) {
            if (!basketMerchantList.contains(merchantBasket.getMerchantId())) {
                basketMerchantList.add(merchantBasket.getMerchantId());
            }
        }
        if (StringUtils.isEmpty(basketList))
            return Rsp.fail();
        return Rsp.success(getNewList(userId,basketList,basketMerchantList));
    }

    public List<MerchantBasket> getNewList(Integer userId,List<MerchantBasket> baskets,List<Integer> basketMerchantList) {
        List<MerchantBasket> newList = new ArrayList<>();
//        Iterator<MerchantBasket> iterator = baskets.iterator();
        for (MerchantBasket merchantBasket : baskets) {
            if (basketMerchantList.contains(merchantBasket.getMerchantId())) {
                MerchantBasket mer = new MerchantBasket();
                mer.setMerchantId(merchantBasket.getMerchant().getId());
                mer.setMerchantName(merchantBasket.getMerchant().getMerchantName());
                mer.setMerchantAddress(merchantBasket.getMerchant().getMerchantAddress());
                mer.setChildMerchantBaskets(merchantBasketMapper.getBasketListByMerchantId(mer.getMerchantId(),userId));
                newList.add(mer);
//                newList.addAll(merchantBasketMapper.getBasketListByMerchantId(basketMerchantList));
            }
        }
        return newList.stream().distinct().collect(Collectors.toList());
    }

    public Rsp changeItem(Integer userId, MerchantBasket merchantBasket) {
        Integer res = merchantBasketMapper.changeItem(userId, merchantBasket);
        return res > 0 ? Rsp.success(res) : Rsp.fail();
    }

    /**
     * 用户访问商户添加商品
     *
     * @param id
     * @param merchantGoods
     * @return
     */
    public MerchantBasket addGoodsToCart(Integer id, MerchantGoods merchantGoods) {
        MerchantBasket merchantBasket = new MerchantBasket();
        merchantBasket.setUserId(id);
        merchantBasket.setMerchantId(merchantGoods.getMerchantId());
        merchantBasket.setGoodsId(merchantGoods.getId());
        merchantBasket.setGoodsName(merchantGoods.getGoodsName());
        merchantBasket.setGoodsPic(merchantGoods.getGoodsImage());
        merchantBasket.setGoodsSize(merchantGoods.getGoodsSpecification());
        merchantBasket.setUnitPrice(merchantGoods.getGoodsSalePrice());
        merchantBasket.setGoodsNum(merchantGoods.getCartCount());
        //查询是否存在该商品
        MerchantBasket merchantBasket1 = merchantBasketMapper.selectGoodsByGoodsId(merchantBasket.getGoodsId(),id,merchantBasket.getMerchantId());
        Integer isInput = 0;
        //判断是否购物车数量为0
        if (merchantBasket.getGoodsNum() == 0) {
            merchantBasketMapper.deleteById(merchantBasket1.getId());
            return null;
        } else {
            if (StringUtils.isEmpty(merchantBasket1)) {
                messageFeign.merchantSend(new Message("用户将"+merchantGoods.getGoodsName()+"添加购物车",merchantGoods.getMerchantId()+""));
                merchantBasketMapper.insert(merchantBasket);
            } else {
                merchantBasket.setId(merchantBasket1.getId());
                isInput = merchantBasketMapper.updateById(merchantBasket);
            }
        }
        return merchantBasketMapper.selectGoodsByGoodsId(merchantBasket.getGoodsId(),id,merchantBasket.getMerchantId());
    }

    public List<MerchantBasket> getCartListByUserId(Integer userId,String merchantId) {
        return  merchantBasketMapper.selectCartListByUserId(userId,Integer.parseInt(merchantId));
    }

    public Integer updateGoodsIndex(MerchantBasket merchantBasket) {
       return merchantBasketMapper.updateById(merchantBasket);
    }

    public Rsp getBasketsCount(Integer userId) {
        Integer basketsCount = merchantBasketMapper.getBasketsCount(userId);
        return Rsp.success(basketsCount);
    }

    public Rsp totalPay(Integer userId, List<String> ids) {
        List<MerchantBasket> basketPriceList = merchantBasketMapper.totalPay(userId, ids);
        double finalMoney = 0.0;
        double totalMoney = 0.0;
        double subtractMoney = 0.0;
        MerchantBasket basketPrice = null;
        for (MerchantBasket merchantBasket : basketPriceList) {
            finalMoney += merchantBasket.getUnitPrice() * merchantBasket.getGoodsNum() - merchantBasket.getConcessionsPrice();
            totalMoney +=  merchantBasket.getUnitPrice() * merchantBasket.getGoodsNum();
            subtractMoney += merchantBasket.getConcessionsPrice();
        }
        basketPrice = new MerchantBasket();
        basketPrice.setFinalMoney(finalMoney);
        basketPrice.setTotalMoney(totalMoney);
        basketPrice.setSubtractMoney(subtractMoney);
//        System.out.println(finalMoney + "," + totalMoney + "," + subtractMoney);
        return Rsp.success(basketPrice);
    }

    public Rsp deleteItem(List<String> ids) {
        Integer res = merchantBasketMapper.deleteItem(ids);
        return res > 0 ? Rsp.success(res) : Rsp.fail();
    }
}
